<template>
  <div class="login-page">
    <!-- 背景层 -->
    <div class="login-bg">
      <div class="bg-gradient"></div>
      <div class="bg-pattern"></div>
    </div>

    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 系统Logo和标题 -->
      <div class="login-header">
        <div class="system-logo">
          <i class="el-icon-truck"></i>
        </div>
        <h1 class="system-title">特种车辆设备故障智能分析系统</h1>
        <p class="system-subtitle">欢迎使用，请先登录</p>
      </div>

      <!-- 登录表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <!-- 用户名输入 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            @keyup.enter="handleLogin"
            class="login-input"
          />
        </el-form-item>

        <!-- 密码输入 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            @keyup.enter="handleLogin"
            class="login-input"
          />
        </el-form-item>

        <!-- 记住密码和忘记密码 -->
        <div class="form-actions">
          <el-checkbox v-model="loginForm.remember" class="remember-me">
            记住密码
          </el-checkbox>
          <el-link type="primary" @click="handleForgotPassword" class="forgot-password">
            忘记密码?
          </el-link>
        </div>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            :loading="loading"
            class="login-btn"
          >
            <span v-if="!loading">登录系统</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
import { ElMessage } from 'element-plus';

const router = useRouter();
const store = useStore();

// 表单数据
const loginForm = ref({
  username: '',
  password: '',
  remember: false,
});

// 表单校验规则
const loginRules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3-20 之间', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      min: 6,
      message: '密码长度不能少于6位',
      trigger: 'blur',
    },
    {
      pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/,
      message: '密码需包含字母和数字',
      trigger: 'blur',
    },
  ],
});

// 加载状态
const loading = ref(false);
const loginFormRef = ref(null);

// 处理登录逻辑
const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      // 固定的用户名和密码
      const fixedUsername = 'admin';
      const fixedPassword = 'admin123456';

      // 验证用户名和密码
      if (loginForm.value.username === fixedUsername && loginForm.value.password === fixedPassword) {
        loading.value = true;
        setTimeout(() => { // 模拟异步请求
          loading.value = false;
          if (loginForm.value.remember) {
            localStorage.setItem('username', loginForm.value.username);
          } else {
            localStorage.removeItem('username');
          }
          router.push({ path: '/overview/home' });
          ElMessage.success('登录成功');
        }, 1000); // 延迟1秒模拟网络请求
      } else {
        ElMessage.error('用户名或密码错误');
      }
    }
  });
};

// 处理忘记密码逻辑
const handleForgotPassword = () => {
  ElMessage.info('请联系管理员重置密码');
  // 实际项目中可改为跳转密码重置页面
  // router.push('/forgot-password');
};
</script>


<style scoped>
.login-page {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 背景样式 */
.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}
.system-subtitle {
  margin: 0;
  font-size: 14px;
  color: #86909c;
}

.bg-gradient {
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #1b5895 0%, #0f3c6d 100%);
}

.bg-pattern {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

/* 登录卡片样式 */
.login-card {
  position: relative;
  z-index: 2;
  width: 420px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  transition: transform 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
}

/* 头部样式 */
.login-header {
  padding: 30px 30px 15px;
  text-align: center;
}

.system-logo {
  width: 60px;
  height: 60px;
  margin: 0 auto 20px;
  background: #1a5fa3;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 28px;
}

.system-title {
  margin: 0 0 10px;
  font-size: 20px;
  font-weight: 600;
  color: #1d2129;
}

.system-subtitle {
  margin: 0;
  font-size: 14px;
  color: #86909c;
}

/* 表单样式 */
.login-form {
  padding: 15px 30px 20px;
}

.login-input {
  height: 46px;
  border-radius: 8px;
  border-color: #dcdfe6;
  transition: all 0.3s ease;
}

.login-input:focus {
  border-color: #1a5fa3;
  box-shadow: 0 0 0 2px rgba(26, 95, 163, 0.1);
}

.form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  font-size: 14px;
}

.remember-me {
  color: #4e5969;
}

.forgot-password {
  font-size: 14px;
  color: #1a5fa3;
}

.login-btn {
  width: 100%;
  height: 46px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(90deg, #1a5fa3 0%, #2c84c9 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-btn:hover {
  background: linear-gradient(90deg, #164c80 0%, #2472b3 100%);
  transform: translateY(-2px);
}

.login-btn:active {
  transform: translateY(0);
}

/* 页脚样式 */
.login-footer {
  padding: 15px 30px;
  text-align: center;
  background: #f7f8fa;
  border-top: 1px solid #f2f3f5;
}

.login-footer p {
  margin: 0;
  font-size: 12px;
  color: #86909c;
}
</style>